﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Biotech.Utils
{
    public class iniUtil
    {
        
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="path">文件路径</param>
        public static void INIWrite(string section, string key, string value, string path)
        {
            WritePrivateProfileString(section, key, value, path);
        }

        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key"></param>
        /// <param name="path">文件路径</param>
        /// <returns>value</returns>
        public static string INIRead(string section, string key, string path)
        {
            StringBuilder temp = new StringBuilder(255);
            GetPrivateProfileString(section, key, "", temp, 255, path);
            if (temp.Length<=0)
            {
                throw new Exception($" 【ini文件缺失或异常】:[{section}] [{key}]");

            }
            else
            {
                return temp.ToString();
            }
            
        }


    }
}
